<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>recaptcha+rsa+aes联合使用</title>
    <script src="https://www.recaptcha.net/recaptcha/api.js" async defer></script>
    <script type="text/javascript" th:src="@{~/js/jquery-2.2.3.min.js}"></script>
    <script type="text/javascript" th:src="@{~/js/jsencrypt.min.js}"></script>
    <script type="text/javascript" th:src="@{~/js/aes.js}"></script>
    <script type="text/javascript" th:src="@{~/js/pad-zeropadding.js}"></script>
    <script type="text/javascript" th:src="@{~/js/security.js}"></script>
    <script type="text/javascript" th:src="@{~/js/random.js}"></script>
    <script>
        function onSubmit(token) {
            //document.getElementById("demo-form").submit();
            // 获取16位随机数,当做aes秘钥key
            var randomStr = randomString(16);
            // aes加密
            var data = Encrypt(JSON.stringify({name:$("#name").val(),password:$("#password").val(),code:token}),randomStr);
            // rsa加密
            var encrypt = new JSEncrypt();
            encrypt.setPublicKey($("#servicePublicKey").val());
            var encrypted = encrypt.encrypt(randomStr);
            // 创建json对象
            var json = {
                "requestData": data,
                "encrypted": encrypted
            };
            $.ajax({
                type: "POST",
                url:"/check3",
                data: JSON.stringify(json),
                dataType:'json',
                contentType: "application/json",
                success: function(result) {
                    alert("返回的数据对象:"+result);
                    // rsa解密,获取到aes秘钥
                    var decrypt = new JSEncrypt();
                    decrypt.setPrivateKey($('#clientPrivateKey').val());
                    var aesKey = decrypt.decrypt(result.encrypted);
                    // 用aes秘钥进行解密
                    var resultData = Decrypt(result.requestData,aesKey);
                    alert("解密的数据：" + resultData);
                }
            });
        }
    </script>
</head>
<body>
<form  method="POST">
    <input type="test" name="name" id="name">
    <input type="test" name="password" id="password">
    <!-- data-sitekey 需要填写公钥 -->
    <button class="g-recaptcha" data-sitekey="6LeYXHsUAAAAAD24u8gTuBUdr8I8uG4brHtpx5z8" data-callback='onSubmit'>Submit</button>
    <br/>
</form>
<h2>服务端公钥：</h2>
<textarea id="servicePublicKey" rows="5" cols="45">MIGfMA0GCSqGS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y9LuBmkzrCuy0tlNXA==
</textarea>
</body>
</html>